package com.crm.web.util;

import org.apache.tapestry.form.IPropertySelectionModel;

import java.util.List;
import java.util.ArrayList;

import com.crm.model.Listbox;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class ListboxPropertySelectionModel implements IPropertySelectionModel {

    private List<Listbox> listbox;

    public ListboxPropertySelectionModel(List<Listbox> listbox){
        if (listbox == null) {
            Listbox list = new Listbox();
            list.setId("0");
            list.setName("--- EMPTY ---");
            this.listbox = new ArrayList<Listbox>(1);
            this.listbox.add(list);
        } else {
            this.listbox = listbox;
        }
    }

    public int getOptionCount() {
        return listbox.size();
    }

    public Object getOption(int index) {
        return listbox.get(index);
    }

    public String getLabel(int index) {
        return listbox.get(index).getName();
    }

    public String getValue(int index) {
        return listbox.get(index).getId();
    }

    public boolean isDisabled(int i) {
        return false;  
    }

    public Object translateValue(String value) {
        Object o = null;
        for (Listbox obj : listbox) {
            if (obj.getId().equals(value)) {
                o = obj;
            }
        }
        return o;
    }
}
